package view;

import java.awt.*;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

import javax.swing.*;
import javax.swing.LayoutStyle.*;
import javax.swing.GroupLayout.*;

import controller.*;

import model.*;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ReservaModificar extends JPanel {
	private static final long serialVersionUID = 1L;
	private JComboBox<String> reservaListaBox;
	private List<Reserva>reservas;
	private Hashtable<String,Reserva>reservasTabla=new Hashtable<String,Reserva>();

	/**
	 * Create the panel.
	 */
	public ReservaModificar(final Persona persona) {
		setSize(new Dimension(800, 400));
		
		JLabel reservaCopiaAltaTit = new JLabel("Modificar Reserva");
		reservaCopiaAltaTit.setAlignmentX(Component.CENTER_ALIGNMENT);
		reservaCopiaAltaTit.setHorizontalAlignment(SwingConstants.CENTER);
		reservaCopiaAltaTit.setHorizontalTextPosition(SwingConstants.CENTER);
		reservaCopiaAltaTit.setFont(new Font("Tahoma", Font.BOLD, 15));
		
		JLabel reservaCopiaAltaNombre = new JLabel("Reserva:");
		reservaCopiaAltaNombre.setHorizontalAlignment(SwingConstants.RIGHT);
		reservaListaBox = new JComboBox<String>();
		if(persona.getClass().getName().contains("PersonaEmpleado"))reservas=ReservaController.getReservaController().listarActivas();
		else{
			reservas=new ArrayList<Reserva>();
			reservas.add(ReservaController.getReservaController().buscarActiva(persona.getId()));
		}
		if(reservas.isEmpty())reservaListaBox.addItem("No hay reservas");
		else{
			for(Reserva reserva : reservas){
				reservasTabla.put(reserva.getUsuario().getNombre()+" - "+reserva.getFechaReserva(), reserva);
				reservaListaBox.addItem(reserva.getUsuario().getNombre()+" - "+reserva.getFechaReserva());
			}
		}
		JButton reservaCopiaAltaBtn = new JButton("Modificar");
		reservaCopiaAltaBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(reservaListaBox.getSelectedItem().equals("No hay reservas")){
					JOptionPane.showMessageDialog(null, "No hay reservas para modificar", "Error", JOptionPane.ERROR_MESSAGE);
				}else{
					Reserva reserva=reservasTabla.get(reservaListaBox.getSelectedItem());
					ReservaModificarPopUp popUp=new ReservaModificarPopUp(reserva);
					final JComponent[] inputs = new JComponent[] {
							popUp
					};
					JOptionPane.showMessageDialog(null, inputs, "Modificar Reserva", JOptionPane.PLAIN_MESSAGE);
					try {
						ReservaController.getReservaController().modificar(reserva, popUp);
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					if(persona.getClass().getName().contains("PersonaEmpleado"))reservas=ReservaController.getReservaController().listarActivas();
					else{
						reservas=new ArrayList<Reserva>();
						reservas.add(ReservaController.getReservaController().buscarActiva(persona.getId()));
					}
					reservasTabla.clear();
					reservaListaBox.removeAllItems();
					for(Reserva reserv : reservas){
						reservasTabla.put(reserv.getUsuario().getNombre()+" - "+reserv.getFechaReserva(), reserv);
						reservaListaBox.addItem(reserv.getUsuario().getNombre()+" - "+reserv.getFechaReserva());
					}
				}
			}
		});
		
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(reservaCopiaAltaNombre, GroupLayout.DEFAULT_SIZE, 316, Short.MAX_VALUE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(reservaCopiaAltaBtn)
								.addComponent(reservaListaBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
							.addContainerGap(395, Short.MAX_VALUE))
						.addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
							.addComponent(reservaCopiaAltaTit, GroupLayout.DEFAULT_SIZE, 800, Short.MAX_VALUE)
							.addContainerGap())))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(reservaCopiaAltaTit, GroupLayout.PREFERRED_SIZE, 19, GroupLayout.PREFERRED_SIZE)
					.addGap(24)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(reservaListaBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(reservaCopiaAltaNombre))
					.addGap(18)
					.addComponent(reservaCopiaAltaBtn)
					.addContainerGap(285, Short.MAX_VALUE))
		);
		setLayout(groupLayout);

	}
}
